package com.kaibo.roomdemo.adapter

import android.databinding.DataBindingUtil
import android.databinding.ViewDataBinding
import android.view.View
import android.view.ViewGroup
import com.chad.library.adapter.base.BaseQuickAdapter
import com.kaibo.roomdemo.R

/**
 * @author Administrator
 * @date 2018/2/14 0014 上午 11:57
 * GitHub：
 * email：
 * description：
 */

abstract class BaseDataBindingAdapter<D, VDB : ViewDataBinding>(layoutResId: Int) : BaseQuickAdapter<D, DataBindingViewHolder<VDB>>(layoutResId) {

    override fun convert(helper: DataBindingViewHolder<VDB>, item: D) {
        val binding = helper.getBinding()
        binding.setVariable(getItemVariableId(), item)
        binding.executePendingBindings()
    }

    abstract fun getItemVariableId(): Int

    override fun getItemView(layoutResId: Int, parent: ViewGroup?): View {
        val binding = DataBindingUtil.inflate<VDB>(mLayoutInflater, layoutResId, parent, false)
        binding?.let {
            it.root.setTag(R.id.BaseQuickAdapter_databinding_support, it)
            return it.root
        }
        return super.getItemView(layoutResId, parent)
    }
}



